package bbs2.dao;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import bbs2.model.Log;

public class LogDAO {
    
    private static Log makeLog(ResultSet rs, boolean includeBody) throws Exception {
        Log log= new Log();
        log.setId(rs.getInt("ID"));
        log.setUserId(rs.getInt("userID"));
        log.setIP(rs.getString("IP"));
        log.setURL(rs.getString("URL"));
        log.setCategory(rs.getString("category"));
        log.setWriteTime(rs.getTimestamp("writeTime"));
        if (includeBody) log.setBody(rs.getString("body"));
        log.setName(rs.getString("name"));
        return log;
    }    
    
    public static ArrayList<Log> selectPage(int currentPage, int pageSize, int order, int srchType, String srchText) throws Exception {
        ArrayList<Log> list = new ArrayList<Log>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "EXEC logSelectPage ?, ?, ?, ?, ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, currentPage);
            statement.setInt(2, pageSize);
            statement.setInt(3, order);
            statement.setInt(4, srchType);
            statement.setString(5, srchText);
            resultSet = statement.executeQuery();
            while (resultSet.next()) 
                list.add(makeLog(resultSet, false));
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return list;
    }
    
    public static int getRecordCount(int srchType, String srchText) throws Exception {
        int count = 0;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "EXEC logRecordCount ?, ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, srchType);
            statement.setString(2, srchText);
            resultSet = statement.executeQuery();
            if (resultSet.next()) 
                count = resultSet.getInt(1);
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return count;
    }
    
    public static void delete(int[] idList) throws Exception {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DB.getConnection();
            String sql = "DELETE FROM [Log] WHERE [ID] = ?"; 
            statement = connection.prepareStatement(sql);
            for (int id : idList) {
                statement.setInt(1, id);
                statement.executeUpdate();
            }
        } finally {
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
    }
    
    public static Log selectById(int id) throws Exception {
        Log log = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "EXEC logSelectByID ?"; 
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) 
                log = makeLog(resultSet, true);
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return log;
    }
    
    public static void insert(int userID, String IP, String URL, Throwable e) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DB.getConnection();
            String sql = "INSERT [Log] ([userID], [IP], [URL], [category], [writeTime], [body]) VALUES (?,?,?,?,GETDATE(),?)"; 
            statement = connection.prepareStatement(sql);
            if (userID > 0) statement.setInt(1, userID);
            else statement.setNull(1, java.sql.Types.INTEGER);
            statement.setString(2, IP);
            statement.setString(3, URL);
            statement.setString(4, e.getClass().getName());
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            statement.setString(5, sw.toString());
            statement.executeUpdate();
        } catch (Exception ex) {
        } finally {
            try {
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (Exception ex) {
            }
        }
    }    

}
